import { RouteRecordRaw, createRouter, createWebHistory } from 'vue-router';
import Cookies from "js-cookie";

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        name: '',
        redirect:"/home",
        component:()=>import('../components/common/layout/index.vue'),
        children:[
            {
                path:"/home",
                meta:{title:"首页"},
                name:"home",
                component:()=>import('/@/views/home.vue'),
            }
        ]
    },
    {
        path:'/user',
        name:"user",
        meta:{title:"用户管理"},
        redirect:"/user/userList",
        component:()=>import('../components/common/layout/index.vue'),
        children:[
            {
                path:"/user/userList",
                name:"userList",
                meta:{title:"用户列表"},
                component:()=>import('/@/views/user/user/index.vue'),
            },
            {
                path:"/user/:id",
                name:"userDetail",
                meta:{title:"用户详细"},
                component:()=>import('/@/views/user/user/detail.vue'),
            },
            {
                path:"/user/manageList",
                name:"manageList",
                meta:{title:"管理员列表"},
                component:()=>import('/@/views/user/manage/index.vue'),
            }
        ]
    },
    {
        path:'/data',
        name:"data",
        meta:{title:"数据管理"},
        redirect:"/data/order",
        component:()=>import('../components/common/layout/index.vue'),
        children:[
            {
                path:"/data/orderList",
                name:"orderList",
                meta:{title:"订单管理"},
                component:()=>import('/@/views/data/order/index.vue'),
            },
            {
                path:"/data/measureList",
                name:"measureList",
                meta:{title:"测量数据"},
                component:()=>import('/@/views/data/measure/index.vue'),
            }
        ]
    },
    {
        path:'/article',
        name:"article",
        meta:{title:"文章管理"},
        redirect:"/article/list",
        component:()=>import('../components/common/layout/index.vue'),
        children:[
            {
                path:"/article/list",
                name:"articleList",
                meta:{title:"文章列表"},
                component:()=>import('/@/views/article/index.vue'),
            },
            {
                path:"/article/edit",
                name:"articleEdit",
                meta:{title:"编辑文章"},
                component:()=>import('/@/views/article/edit.vue'),
            }
        ]
    },
    {
        path:'/system',
        name:"system",
        redirect:"/system/info",
        meta:{title:"系统设置"},
        component:()=>import('../components/common/layout/index.vue'),
        children:[
            {
                path:"/system/info",
                name:"info",
                meta:{title:"基本信息"},
                component:()=>import('/@/views/system/info.vue'),
            },
        ]
    },
    {
        path:'/message',
        name:"message",
        meta:{title:"消息中心"},
        redirect:"/message/list",
        component:()=>import('../components/common/layout/index.vue'),
        children:[
            {
                path:"/message/list",
                name:"messageList",
                meta:{title:"消息列表"},
                component:()=>import('/@/views/message/index.vue'),
            },
            {
                path:"/message/:id",
                name:"messageDetail",
                meta:{title:"消息详细"},
                props:true,
                component:()=>import('/@/views/message/detail.vue'),
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component:()=>import('/@/components/common/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        component:()=>import('/@/components/common/extra/403.vue'),
    },
    {
        path: "/:catchAll(.*)",
        name: '404',
        component:()=>import('/@/components/common/extra/404.vue'),
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    next()
    // if (Cookies.get('userInfo')) {  //如果用户已经登录
    //     if (to.path == "/login") {  //如果用户已经登录且跳转前往登录页面
    //         next({ name: "home" })
    //     } else {  //如果用户跳转的是其他页面则判断其是否拥有权限
    //         next()
    //     }
    // } else {
    //     if(to.path !== "/login"){
    //         window.location.href="https://sso.yolanda.hk/login?id=1000036&back="+encodeURIComponent(window.location.href)
    //     }
    //     if (to.path == "/login") {  //如果用户已经登录且跳转前往登录页面
    //         next()
    //     } else {  //如果用户跳转的是其他页面则判断其是否拥有权限
    //         next({ name: "login" })
    //     }
    // }
})

export default router;